<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" v-if="hasPermission('oa_city_coor_create')" @click="handleCreate">
          新增
        </a-button>
        <!-- action="/server/oa_city_coor/import" -->
        <!-- :headers="{ Authorization: 'Bearer' + token }" -->
        <Upload
          accept=".xlsx,.xls"
          :show-upload-list="false"
          v-if="hasPermission('oa_city_coor_import')"
          :customRequest="loadDataSuccess"
        >
          <a-button type="primary" ghost> 导入 </a-button>
        </Upload>
        <a-button type="primary" @click="handleTemplet"> 导出模板 </a-button>
        <a-button type="primary" v-if="hasPermission('oa_city_coor_export')" @click="handleExport">
          导出列表
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'oa_city_coor_edit',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              auth: 'oa_city_coor_delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否删除?',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <OaCityCoorModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { deleteApi, pageApi, templetApi, exportApi, importApi } from '/@/api/business/oaCityCoor';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import OaCityCoorModal from './OaCityCoorModal.vue';
  import { columns, searchFormSchema } from './oaCityCoor.data';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { Upload } from 'ant-design-vue';
  import { getToken } from '/@/utils/auth';

  export default defineComponent({
    name: 'OaCityCoor',
    components: { Upload, BasicTable, OaCityCoorModal, TableAction },
    setup() {
      const { hasPermission } = usePermission();
      const { createMessage } = useMessage();
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, expandAll, getForm }] = useTable({
        title: '城市坐标列表',
        api: pageApi,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        isTreeTable: true,
        pagination: true,
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        canResize: false,
        rowKey: 'id',
        // defSort: {
        //   field: 'city',
        //   order: 'ascend',
        // },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });
      const token = getToken();
      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await deleteApi(record);
        createMessage.success('删除成功');
        reload();
      }

      function onFetchSuccess() {
        // 演示默认展开所有表项
        nextTick(expandAll);
      }

      function handleSuccess() {
        reload();
      }

      async function loadDataSuccess(file) {
        const result = await importApi(file.file);
        console.log('result', result);
        createMessage.success('导入成功');
        reload();
      }

      async function handleTemplet() {
        await templetApi();
      }

      async function handleExport() {
        const form = getForm();
        const fields = form.getFieldsValue();
        await exportApi(fields);
      }

      return {
        hasPermission,
        registerTable,
        registerModal,
        onFetchSuccess,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        loadDataSuccess,
        handleTemplet,
        handleExport,
        importApi,
        token,
      };
    },
  });
</script>
